package com.justgame.project.service.system.query;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.justgame.project.cloud.common.query.Column;
import com.justgame.project.cloud.common.query.BaseSelectBuilder;
import com.justgame.project.cloud.common.query.AbstractQueryWrapper;
import com.justgame.project.cloud.common.query.Query;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.justgame.project.service.system.entity.SysUserRoleMapping;
import com.justgame.project.service.system.query.SysRoleInfoQuery;
import com.justgame.project.service.system.query.SysUserInfoQuery;
import com.justgame.project.service.system.entity.SysUserInfo;
import com.justgame.project.service.system.entity.SysRoleInfo;
import com.justgame.project.service.system.wrapper.SysUserRoleMappingWrapper;

public class SysUserRoleMappingQuery extends AbstractQueryWrapper<SysUserRoleMapping, MPJLambdaWrapper<SysUserRoleMapping>, SysUserRoleMappingWrapper> {

    private SysUserRoleMappingQuery() {
        super(new MPJLambdaWrapper<>(SysUserRoleMapping.class,"sys_user_role_mapping"));
    }

    public static SysUserRoleMappingQuery builder(){
        return new SysUserRoleMappingQuery();
    }

    public static class Select{

        private final List<Query> queries;

        protected Select(){
            queries = new ArrayList<>();
        }

        protected Select(List<Query> queries) {
            this.queries = queries;
        }

        public Query[] getQueriesArray() {
            return queries.toArray(new Query[0]);
        }

        public static SelectBuilder builder(){
            return new SelectBuilder();
        }

        public Query[] build(){
            return queries.toArray(new Query[0]);
        }

        public static class SelectBuilder extends BaseSelectBuilder {
            public SelectBuilder SysUserRoleMapping(){
                return SysUserRoleMapping(Column.all());
            }

            public SelectBuilder SysUserRoleMapping(Column column){
                return add(Query.builder(SysUserRoleMapping.class)
                            .selectColumns(column)
                            .build());
            }

            public SelectBuilder sysUserInfo(){
                return sysUserInfo(Column.all());
            }

            public SelectBuilder sysUserInfo(Column column){
                return sysUserInfo(column, null);
            }

            public SelectBuilder sysUserInfo(Column column,SysUserInfoQuery.Select selectQuery){
                return sysUserInfo("sysUserInfoWrapper", column, null);
            }

            public SelectBuilder sysUserInfo(String mappingName,Column column,SysUserInfoQuery.Select selectQuery){
                return add(Query.builder(SysUserInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<SysUserInfo,?>)SysUserInfo::getId)
                            .rightOn((SFunction<SysUserRoleMapping,?>)SysUserRoleMapping::getUserInfoId)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(SysUserInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public SelectBuilder sysRoleInfo(){
                return sysRoleInfo(Column.all());
            }

            public SelectBuilder sysRoleInfo(Column column){
                return sysRoleInfo(column, null);
            }

            public SelectBuilder sysRoleInfo(Column column,SysRoleInfoQuery.Select selectQuery){
                return sysRoleInfo("sysRoleInfoWrapper", column, null);
            }

            public SelectBuilder sysRoleInfo(String mappingName,Column column,SysRoleInfoQuery.Select selectQuery){
                return add(Query.builder(SysRoleInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<SysRoleInfo,?>)SysRoleInfo::getId)
                            .rightOn((SFunction<SysUserRoleMapping,?>)SysUserRoleMapping::getRoleId)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(SysRoleInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public Select build(){
                return new Select(getQueries());
            }
        }

    }
}